|
|
|
|
|
|
|
As an example, redo the demo that you created with the Data control. Start a new project and add two text boxes to the default form to hold the data from the query, as shown in Figure 10.7. |
|
|
|
|
|
|
|
|
Figure 10.7.
Using unbound controls to
display data from the Data
Access Objects. |
|
|
|
|
|
|
|
|
To set up the query for use, you must open a database connection and then open a recordset by using the Open method. In the General Declarations section of the form, the Dim statements that define the ActiveX Data Objects are needed by the code in the form. You then open the connection and query in the Form_Load event routine, as shown in Listing 10.1. |
|
|
|
|
|
|
|
|
LISTING 10.1. ADOEX1.TXTOPENING THE DATABASE AND RECORDSET IN THEForm_LoadEVENT. |
|
|
|
 |
|
|
|
|
1: strCnn = driver={SQL Server} ;server=srv; & _
2: uid=sa;pwd=;database=pubs
3: Set cnn1 = New ADODB.Connection
4: cnn1.Open strCnn
5: MyDyn.Open(Select * From Authors, cnn1,,,adCmdText)
6: MyDyn.MoveFirst
7: Call DisplayFields |
|
|
|
|
|
|
|
|
Now, the query is open and you're positioned at the first record in the recordset. To display the data, assign the value of the desired data fields to the display properties of the controls containing the data. For this type of processing, it's recommended that you create a subroutine (as shown in Listing 10.2) to perform the assignment of the controls from the current record in the recordset. This same routine can be called from a number of command button events rather than repeat the code in each event. This way, the code is more efficient and easier to maintain. |
|
|
|
|
|